<?php

class OpusController extends BackendController
{

    public $layout='/layouts/column2';
    public $city_en;
    public function actionIndex()
    {
        header("Content-type: text/html; charset=utf-8");
        $model = new Opus();
        $criteria = new CDbCriteria;
        $criteria->addCondition("opus_source_type='课堂资源'");
        $criteria->order = "id DESC";
        $dataProvider= new CActiveDataProvider($model, array(
            'criteria' => $criteria,
            'pagination'=>array(
                'pageSize'=>20,
            ),
        ));
        $this->render('index',array('dataProvider'=>$dataProvider));
    }
    public function actionStudent()
    {
        header("Content-type: text/html; charset=utf-8");
        if(Yii::app()->request->isPostRequest){
            $code_num = $_POST['code_num'];
            $user_name = $_POST['user_name'];
            $gid = $_POST['gid'];
            $cid = $_POST['cid'];
            $where = " WHERE t1.opus_source_type='作品上传'";
            if($code_num){
                $where .= " AND t2.code_num='$code_num' ";
            }
            if($user_name){
                $where .= " AND t2.user_name='$user_name' ";
            }
            if($gid){
                $where .= " AND t2.gid='$gid' ";
            }
            if($cid){
                $where .= " AND t2.cid='$cid' ";
            }
            $jiaDb = Yii::app()->db->createCommand("SELECT * FROM tbl_opus t1 LEFT JOIN tbl_people t2 ON t2.id=t1.st_id $where");
            $dataProvider = $jiaDb->queryAll();
            $grade = Grade::model()->findAll('is_show=:is_show and status=:status',array(':is_show'=>1,':status'=>1));
            $this->render('index2',array('dataProvider'=>$dataProvider,'grade'=>$grade));
        }else{
            $grade = Grade::model()->findAll('is_show=:is_show and status=:status',array(':is_show'=>1,':status'=>1));
            $model = new Opus();
            $criteria = new CDbCriteria;
            $criteria->addCondition("opus_source_type='作品上传'");
            $criteria->order = "id DESC";
            $dataProvider= new CActiveDataProvider($model, array(
                'criteria' => $criteria,
                'pagination'=>array(
                    'pageSize'=>20,
                ),
            ));
            $this->render('index1',array('dataProvider'=>$dataProvider,'grade'=>$grade));
        }
    }
    public function isThumb($data){
        $str = '';
        if($data->thumb !=''){
            $str .= '<img style="width: 100%" src="'.Yii::app()->params['webPath'].$data->thumb.'" />';
        }
        return $str;
    }

    public function actionDelete(){
        $id = Yii::app()->request->getParam('id');
        $info = Opus::model()->findByPk($id);
        if($info){
            $ok = $info->delete();
            if($ok){
                $data['status'] = 1;
                $data['message'] = '删除数据成功';
            }else{
                $data['status'] = 0;
                $data['message'] = '删除数据失败';
            }

        }else{
            $data['status'] = 0;
            $data['message'] = '你无权作此操作';
        }
        echo json_encode($data);
        Yii::app()->end();
    }

    public function actionUpdate(){
        $id = Yii::app()->request->getParam('id');
        Yii::app()->clientScript->registerScriptFile($this->adminPath . '/js/jquery.min.js',CClientScript::POS_HEAD);
        //获取区域
        $info = Opus::model()->findByPk($id);
        $info1 = OpusList::model()->find('opus_id=:opus',array(':opus'=>$id));
        if(Yii::app()->request->isPostRequest){
            if($info){
                $info->setAttributes($_POST['Opus']);
                if($info->update()){
                    $file = $_POST['file'];
                    $info1->opus_path = $file;
                    $info1->update();
                    //写入周边
                    $data['status'] = 1;
                    $data['message'] = '修改成功';
                }else{
                    $data['status'] = 0;
                    $data['message'] = $this->showError($info->getErrors());
                }
            }else{
                $data['status'] = 0;
                $data['message'] = '你无权作此操作';
            }
            echo json_encode($data);
            Yii::app()->end();
        }
        $this->layout = '/layouts/dialog';

        $this->render('update',array('info'=>$info,'info1'=>$info1));
    }
    public function isSystem($data){
        $str = '';
//        $str .= '<a onclick="zanInfo(\''.$data->id.'\')"  href="javascript:void(0);" style="color:red;" >点赞管理</a><br/>';
        $str .= '<a onclick="pinglunInfo(\''.$data->id.'\')"  href="javascript:void(0);" style="color:green;" >评论管理</a><br/>';
        return $str;
    }
    public function actionAdd(){
        if(Yii::app()->request->isPostRequest){
            $_POST['Opus']['create_time'] = date('Y-m-d H:i:s',time());
            $model = new Opus();
            $model->setAttributes($_POST['Opus']);
            $file = $_POST['file'];
            if($model->save()){
                $data['status'] = 1;
                $id = $model->attributes['id'];
                $cc = new OpusList();
                $cc->opus_id = $id;
                $cc->opus_path = $file;
                $cc->save();
                $data['message'] = '发布成功';
            }else{
                $data['status'] = 0;
                $data['message'] = $this->showError($model->getErrors());
            }
            echo json_encode($data);
            Yii::app()->end();
        }else{
            Yii::app()->clientScript->registerScriptFile($this->adminPath . '/js/jquery.min.js',CClientScript::POS_HEAD);
            $this->layout = '/layouts/dialog';
            $this->render('add');
        }
    }
    public function isManage($data){
        $str = '';
        $str .= '<a onclick="jifenInfo(\''.$data->id.'\')"  href="javascript:void(0);" style="color:red;" >操作1</a><br/>';
        $str .= '<a onclick="jiazhangInfo(\''.$data->id.'\')"  href="javascript:void(0);" style="color:green;" >操作2</a><br/>';
        return $str;
    }
    public function isStatus($data){
        $str = '';
        if($data->is_book==1){
            $str .='<span style="color: green">必读</span>';
        }else{
            $str .='非必读';
        }
        $str .= '<br/>';
        if($data->is_recommend==1){
            $str .='<span style="color: green">推荐</span>';
        }else{
            $str .='非推荐';
        }
        return $str;
    }

}